import {Alert} from "react-native";
import Alipay from "react-native-gzdsx-alipay";
import ApiService from "../../utils/ApiService";

export default {
    cancel(order_id, reason) {
        return ApiService.post('/trade/boughts/' + order_id + '/cancel', {reason});
    },
    delete(order_id) {
        return new Promise((resolve, reject) => {
            Alert.alert('确定要删除此订单吗?', null, [
                {
                    text: '确定',
                    onPress: () => {
                        ApiService.delete('/trade/boughts/' + order_id).then(response => {
                            resolve(response);
                        }).catch(reason => {
                            reject(reason);
                        });
                    }
                },
                {
                    text: '取消',
                    onPress: (val) => {
                        reject(val);
                    }
                }
            ])
        });
    },
    notice(order_id) {
        return ApiService.post('/trade/boughts/' + order_id + '/notice', {order_id});
    },
    confirm(order_id) {
        return new Promise((resolve, reject) => {
            Alert.alert('确认收货', '请确认是否已收到货品', [
                {
                    text: '确定',
                    onPress: () => {
                        ApiService.post('/trade/boughts/' + order_id + '/confirm').then(response => {
                            resolve(response);
                        }).catch(reason => {
                            reject(reason);
                        });
                    }
                },
                {
                    text: '取消',
                    onPress: val => {
                        reject(val);
                    }
                }
            ]);
        });
    },
    pay(order_id, type = 'alipay') {
        return new Promise((resolve, reject) => {
            ApiService.post('/trade/orders/paysign', {order_id, type}).then(response => {
                //console.log(response.result);
                Alipay.pay(response.result.payStr).then((data) => {
                    //console.log(data);
                    resolve(data);
                }, (code, domain, message) => {
                    reject({code, message, domain});
                });
            }).catch(reason => {
                reject(reason);
            });
        });
    }
}
